/*
model:baison.slider.js
author:guoxuemin
created:2012-04-10
email:469477762@qq.com
*/
;(function($){
	$.fn.scrollList = function(o){
		var o = $.extend(
			{
			prev:null,
			next:null,
			list:null,
			visible:4,
			scroll:4,
			vertical:false,
			speed:800
			},
			o||{}
		);	
		return this.each(function(){
			var animCss,sizeCss,c,list,box,item,prev,next,size,v,style,scroll=0,scrolled=0;
			animCss = o.vertical ? "scrollTop": "scrollLeft";
			sizeCss = o.vertical ? "height": "width";
			c = $(this);
			prev = $(o.prev,c);
			next = $(o.next,c);
			list = $(o.list,c);
			eval("list."+animCss+"(0)");
			box = list.children();
			item = box.children(":visible");
			size = item.size();
			v = o.visible;
			style = o.vertical ? height(item):width(item);
			list.css(sizeCss,v*style);
			box.css(sizeCss,size*style);
			scroll = o.vertical ? "scrollHeight": "scrollWidth";
			scroll = eval('list[0].'+scroll)-v*style;
			prev.unbind();
			next.unbind();
			if(scroll>0){
				next.addClass("next-disabled");
				prev.bind("click",prevFun);
				next.bind("click",nextFun);
			}else{
				prev.addClass("prev-disabled");
				next.addClass("next-disabled");
				return;
			}
			function prevFun(){
				list.stop(false,true).animate(animCss == 'scrollLeft' ? {'scrollLeft':"+="+(o.scroll*style)}:{'scrollTop':"+="+(o.scroll*style)},o.speed);
				next.removeClass('next-disabled');
				scrolled += o.scroll*style;
				if(scrolled >= scroll){
					$(this).addClass('prev-disabled');
					return false;
				};
			}
			function nextFun(){
				list.stop(false,true).animate(animCss == 'scrollLeft' ? {'scrollLeft':"-="+(o.scroll*style)}:{'scrollTop':"-="+(o.scroll*style)},o.speed);
				prev.removeClass('prev-disabled');
				scrolled -= o.scroll*style;
				if(scrolled <= 0){
					next.addClass('next-disabled');
					return false;
				};
			}
		});
		
	}

	function width(elem){
		return elem[0].offsetWidth+ parseInt(elem.css('marginLeft'))+parseInt(elem.css('marginRight'));
	};
	function height(elem){
		return elem[0].offsetHeight+ parseInt(elem.css('marginTop'))+parseInt(elem.css('marginBottom'));
	};
})(jQuery);